package com.le.tester.onjava8.l_2_2_4;

/**
 * createTime：2023/1/6 14:25
 * description：克隆副本
 */

class Duplo implements Cloneable {
    private int n;

    Duplo(int n) {
        this.n = n;
    }

    @Override
    public Duplo clone() {
        try {
            return (Duplo) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }

    public int getValue() {
        return n;
    }

    public void setValue(int n) {
        this.n = n;
    }

    public void increment() {
        n++;
    }

    @Override
    public String toString() {
        return Integer.toString(n);
    }
}

public class LocalCopy implements Cloneable {
    public static Duplo g(Duplo v) {
        v.increment();
        return v;
    }

    public static Duplo f(Duplo v) {
        v = v.clone();
        v.increment();
        return v;
    }

    public static void main(String[] args) {

//        String newStr = "12:00,12:30,13:00,13:30,14:00,14:30,15:00,15:30,16:00,16:30,17:00,17:30,18:00,18:30,19:00,19:30,20:00,20:30";
//        String oldStr = "09:00";
//        if (!newStr.contains(oldStr)) {
//            System.out.println("true");
//        } else {
//            System.out.println("false");
//        }

        Duplo a = new Duplo(11);
        //没有new一个新的地址，就是使用相同的引用
        Duplo b = g(a);
        System.out.println("a==b: " + (a == b) + "\na = " + a + "\nb= " + b);

        Duplo c = new Duplo(47);
        Duplo d = f(c);
        System.out.println("c==d: " + (c == d) + "\nc = " + c + "\nd= " + d);

    }

}
